package com.gs.wechatdemo.audio;

import android.content.Context;
import android.net.Uri;
import android.widget.Toast;

import java.io.File;

public class AudioPresenter<T extends IAudioView.View> implements IAudioView.Presenter {

    private static final int MAX_VOICE_TIME = 20;
    private static final String AUDIO_DIR_NAME = "audio_cache";

    private T mView;
    private Context context;
    private File mAudioDir;

    public AudioPresenter(T mView, Context context) {
        this.mView = mView;
        this.context = context;
        mAudioDir = new File(context.getExternalCacheDir(), AUDIO_DIR_NAME);
        if (!mAudioDir.exists()) {
            mAudioDir.mkdirs();
        }
    }

    @Override
    public void init() {
        initAudioManager();
    }

    @Override
    public void startRecord() {
        AudioRecordManager.getInstance(context).startRecord();
    }

    @Override
    public void stopRecord() {
        AudioRecordManager.getInstance(context).stopRecord();
    }

    @Override
    public void willCancelRecord() {
        AudioRecordManager.getInstance(context).willCancelRecord();
    }

    @Override
    public void continueRecord() {
        AudioRecordManager.getInstance(context).continueRecord();
    }

    @Override
    public void startPlayRecord(String url) {
        AudioPlayManager.getInstance().startPlay(url, new IAudioPlayListener() {
            @Override
            public void onStart(String url) {
            }

            @Override
            public void onStop(String url) {
                mView.finishPlayAudio();
            }

            @Override
            public void onComplete(String url) {
                mView.finishPlayAudio();
            }
        });
    }

    @Override
    public void stopPlayRecord() {
        mView.finishPlayAudio();
        AudioPlayManager.getInstance().stopPlay();
    }

    private void initAudioManager() {
        AudioRecordManager.getInstance(context).setAudioSavePath(mAudioDir.getAbsolutePath());
        AudioRecordManager.getInstance(context).setMaxVoiceDuration(MAX_VOICE_TIME);
        AudioRecordManager.getInstance(context).setAudioRecordListener(new IAudioRecordListener() {
            @Override
            public void initTipView() {
                mView.showNormalTipView();
            }

            @Override
            public void setTimeoutTipView(int counter) {
                mView.showTimeOutTipView(counter);
            }

            @Override
            public void setRecordingTipView() {
                mView.showRecordingTipView();
            }

            @Override
            public void setAudioShortTipView() {
                mView.showRecordTooShortTipView();
            }

            @Override
            public void setCancelTipView() {
                mView.showCancelTipView();
            }

            @Override
            public void destroyTipView() {
                mView.hideTipView();
            }

            @Override
            public void onStartRecord() {

            }

            @Override
            public void onFinish(Uri audioPath, int duration) {
                if (audioPath.getPath() != null) {
                    File file = new File(audioPath.getPath());
                    if (file.exists()) {
                        mView.finishRecord(file.getAbsolutePath());
                    }
                }
            }

            @Override
            public void onAudioDBChanged(int db) {
                mView.updateCurrentVolume(db);
            }
        });
    }

}
